package com.greatchn.no_session_auth_code.config;

import com.greatchn.no_session_auth_code.handler.AccDeniedHandler;
import com.greatchn.no_session_auth_code.handler.AuthEntryPointHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

import javax.annotation.Resource;

/**
 * @A: security config
 * @author: ZBoHang
 * @time: 2023/2/8 9:48
 */
@Configuration
public class CustomSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Resource
    private UserDetailsService userDetailsService;
    @Resource
    private AuthTokenConfig authTokenConfig;
    @Resource
    private AuthEntryPointHandler authEntryPointHandler;
    @Resource
    private AccDeniedHandler accDeniedHandler;
    @Resource
    private FilterMetadataSource filterMetadataSource;
    @Resource
    private AccDecisionManager accDecisionManager;


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                // 允许postman等测试
                .csrf().disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .exceptionHandling()
                .accessDeniedHandler(this.accDeniedHandler)
                .authenticationEntryPoint(this.authEntryPointHandler)
                .and()
                .authorizeRequests()
                // .antMatchers("/login/**").permitAll()
                // .antMatchers("/admin/**").hasRole("ADMIN")
                // .antMatchers("/**")
                .anyRequest()
                .authenticated()
                // 自定义角色权限
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O object)  {
                        // 设置角色权限
                        object.setSecurityMetadataSource(filterMetadataSource);
                        // 判定角色权限
                        object.setAccessDecisionManager(accDecisionManager);
                        return object;
                    }
                })
                .and()
                // 跨域
                .cors().disable()
                .apply(this.authTokenConfig);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 设置自定义的userDetailsService
        auth.userDetailsService(this.userDetailsService)
                .passwordEncoder(passwordEncoder());

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        // TODO: 2023/2/8
        // 不加密
        return NoOpPasswordEncoder.getInstance();
    }

    /**
     * 密码模式需先单独将认证管理器置入ioc容器
     */
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
